﻿#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <thread>

#include "Pool.h"
#include "ObjectPool.h"



static std::mutex outmtx;

auto TestPool(){
    Pool<std::string, 10> pool{ "123456" };

    for (auto i = 0; i < 12; ++i) {
        std::thread([&pool, i]() {
            auto s1 = pool.acquire();
            if (s1)
            {
                *s1 = std::to_string(i);
                outmtx.lock();
                std::cout << *s1 << '\t' << pool.remaining() << '\n';
                outmtx.unlock();
            }
            pool.restore(s1);

        }).detach();
    }


    std::cout << pool.remaining() << '\n';
    getchar();
}

auto TestObjectPool(){
    ObjectPool<std::string, 10> pool{ "123456" };

    for (auto i = 0; i < 12; ++i) {
        std::thread([&pool, i]() {
            auto s1 = pool.acquire();
            if (s1)
            {
                *s1 = std::to_string(i);
                outmtx.lock();
                std::cout << *s1 << '\t' << pool.remaining() << '\n';
                outmtx.unlock();
            }
            pool.restore(s1);

        }).detach();
    }
    std::cout << pool.remaining() << '\n';
    getchar(); // 等待所有线程结束
}

int main()
{
    TestObjectPool();
    return 0;
}